// miniprogram/pages/groupDetail/groupDetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    group: {},
    isJoined: false,
    isLogin: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadData()
  },

  loadData () {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptData', (data) => {
      this.setData({
        group: data.data
      })
      if (app.globalData.userInfo && app.globalData.userInfo.groups) {
        app.globalData.userInfo.groups.forEach(item => {
          if (item.groupId === data.data.groupId) {
            this.setData({
              isJoined: true
            })
          }
        })
        this.setData({
          isLogin: true
        })
      }
    })
  },
  // 判断登录状态
  async goLogin() {
    if (!this.data.isLogin) {
      const { confirm } = await wx.showModal({
        title: '提示',
        content: '请先登录再操作！'
      })
      if (confirm) {
        wx.navigateTo({
          url: '/pages/login/login',
        })
        const waitLogin = () => new Promise((resolve, reject) => {
          const timer = setInterval(() => {
            if (app.globalData.userInfo) {
              clearInterval(timer)
              resolve()
            }
          }, 20)
        }).then(res => {
          this.setData({
            isLogin: true
          })
          this.loadData()
        })
        await waitLogin()
      }
    }
  },
  // 加入学习组
  async handleJoin () {
    await this.goLogin()
    if (!app.globalData.userInfo) {
      return
    } else {
      wx.showLoading({
        title: '加入中...',
        mask: true
      })
      const group = this.data.group
      const { result } = await wx.cloud.callFunction({
        name: 'joinGroup',
        data: group
      })
      if (result.status === 0) {
        await wx.setClipboardData({
          data: this.data.group.groupNum.toString()
        })
        wx.showModal({
          title: '成功',
          content: result.msg
        })
        this.setData({
          isJoined: true
        })
        app.globalData.userInfo.groups.push({
          groupId: group.groupId,
          groupName: group.groupName
        })
      }
      wx.hideLoading()
    }
  },
  // 退出学习组
  async handleExit () {
    await this.goLogin()
    if (!app.globalData.userInfo) {
      return
    } else {
      const exit = await wx.showModal({
        title: '警告',
        content: '确定退出学习组吗？'
      })
      if (exit.cancel) {
        wx.showToast({
          title: '取消退出',
          icon: 'none'
        })
      } else if (exit.confirm) {
        wx.showLoading({
          title: '退出中...',
          mask: true
        })
        const group = this.data.group
        const { result } = await wx.cloud.callFunction({
          name: 'exitGroup',
          data: group
        })
        if (result.status === 0) {
          wx.showToast({
            title: result.msg,
            icon: 'success'
          })
          this.setData({
            isJoined: false
          })
          const newUserGroups = app.globalData.userInfo.groups.filter(item => item.groupId !== group.groupId)
          app.globalData.userInfo.groups = newUserGroups
        }
        wx.hideLoading()
      }
    }
  }
})